package com.demo.controller;

import com.demo.entity.User;
import com.demo.model.JsonMessage;
import com.demo.security.AuthUserDetail;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService service;

    @GetMapping("info")
    @ResponseBody
    public JsonMessage userinfo(Authentication auth) {
        AuthUserDetail detail = (AuthUserDetail) auth.getDetails();
        JsonMessage message = new JsonMessage();
        Map<String, Object> data = new HashMap<>();
        data.put("name", detail.getUsername());
        message.setData(data);
        return message;
    }

    @PostMapping("logout")
    @ResponseBody
    public JsonMessage logout(@RequestHeader("X-Token") String token) {
        JsonMessage message = new JsonMessage();
        return message;
    }


    @PostMapping("findAllUsers")
    @ResponseBody
    public JsonMessage findAllUsers() {
        JsonMessage message = new JsonMessage();
        List<User> list = service.findAllUsers();
        message.setData(list);
        return message;
    }

    @PostMapping("updateUserState")
    @ResponseBody
    public JsonMessage updateUserState(String disabled, int userId) {
        JsonMessage message = new JsonMessage();
        service.updateUserState(disabled, userId);
        message.setData("ok");
        return message;
    }

    @PostMapping("resetUser")
    @ResponseBody
    public JsonMessage resetUser(int userId) {
        JsonMessage message = new JsonMessage();
        service.resetUser(userId);
        message.setData("ok");
        return message;
    }

    @PostMapping("deleteUser")
    @ResponseBody
    public JsonMessage deleteUser(int userId) {
        JsonMessage message = new JsonMessage();
        service.deleteUser(userId);
        message.setData("ok");
        return message;
    }
}
